package cn.com.oceansoft.osc.ms.utils;



import java.util.Locale;

/**
 * 字符串操作工具包 结合android.text.TextUtils使用
 *
 * @author chenw
 * @email javacspring@gmail.com
 * @time 2017/04/13
 */
public final class StringUtil {

    /**
     * byte[]转int
     *
     * @param bytes 字节数组
     * @return int
     */
    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        //由高位到低位
        for (int i = 0; i < 2; i++) {
            int shift = (2 - 1 - i) * 2;
            value += (bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
    }

    /**
     * int转byte数组
     *
     * @param intVal 整数值
     * @return 字节数组
     */
    public static byte[] intToByteArray(int intVal) {
        byte[] result = new byte[4];
        //由高位到低位
        result[0] = (byte) ((intVal >> 24) & 0xFF);
        result[1] = (byte) ((intVal >> 16) & 0xFF);
        result[2] = (byte) ((intVal >> 8) & 0xFF);
        result[3] = (byte) (intVal & 0xFF);
        return result;
    }

    /**
     * Don't let anyone instantiate this class.
     */
    private StringUtil() {
        throw new Error("Do not need instantiate!");
    }

    /**
     * Returns true if the string is null or 0-length.
     *
     * @param str the string to be examined
     * @return true if str is null or zero length
     */


    /**
     * 字符串转整数
     *
     * @param str      数字字符串
     * @param defValue 默认值
     * @return
     */
    public static int strToInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defValue;
    }

    /**
     * byte转int
     *
     * @param bytes 源字节数组
     * @param start 起始字节位置
     * @param len   转换字节数组长度
     * @return int
     */
    public static int bytes2Int(byte[] bytes, int start, int len) {
        int sum = 0;
        int end = start + len;
        for (int i = start; i < end; i++) {
            int n = ((int) bytes[i]) & 0xff;
            n <<= (--len) * 8;
            sum += n;
        }
        return sum;
    }

    /**
     * byte[]数组转换为16进制的字符串
     *
     * @param data 要转换的字节数组
     * @return 转换后的结果
     */
    public static String byteArrayToHexString(byte[] data) {
        if (null == data || data.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder(data.length * 2);
        for (byte b : data) {
            sb.append(" 0x");
            int v = b & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v).toUpperCase(Locale.getDefault()));
        }
        return sb.toString();
    }

    /**
     * byte转16进制字符串
     *
     * @param data 字节
     * @return 16进制字符串
     */
    public static String byteToHexString(byte data) {
        int tmp = data & 0xFF;
        return "0x" + (tmp < 16 ? "0" + Integer.toHexString(tmp) : Integer.toHexString(tmp)).toUpperCase();
    }


    /**
     * byte[]转16进制字符串
     *
     * @return 16进制字符串
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (byte b : src) {
            int v = b & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            stringBuilder.append(" 0x");
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().trim();
    }

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] d = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个进制字节
            d[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return d;
    }

    /**
     * 十六进制字符串转十进制
     *
     * @param hex 十六进制字符串
     * @return 十进制数值
     */
    public static int hexStringToInt(String hex) {
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorithm;
            if (c >= '0' && c <= '9') {
                algorithm = c - '0';
            } else {
                algorithm = c - 55;
            }
            result += Math.pow(16, max - i) * algorithm;
        }
        return result;
    }

    /**
     * 将给定的字符串中所有给定的关键字标红
     *
     * @param sourceString 给定的字符串
     * @param keyword      给定的关键字
     * @return 返回的是带Html标签的字符串，在使用时要通过Html.fromHtml()转换为Spanned对象再传递给TextView对象
     */
    public static String keywordMadeRed(String sourceString, String keyword) {
        String result = "";
        if (sourceString != null && !"".equals(sourceString.trim())) {
            if (keyword != null && !"".equals(keyword.trim())) {
                result = sourceString.replaceAll(keyword,
                        "<font color=\"red\">" + keyword + "</font>");
            } else {
                result = sourceString;
            }
        }
        return result;
    }

    /**
     * 为给定的字符串添加HTML红色标记，当使用Html.fromHtml()方式显示到TextView 的时候其将是红色的
     *
     * @param string 给定的字符串
     * @return String
     */
    public static String addHtmlRedFlag(String string) {
        return "<font color=\"red\">" + string + "</font>";
    }

}
